package com.example.sneakerappserver.controller;

import com.example.sneakerappserver.pojo.Order;
import com.example.sneakerappserver.pojo.OrderItem;
import com.example.sneakerappserver.pojo.ResponseMessage;
import com.example.sneakerappserver.pojo.dto.OrderDto;
import com.example.sneakerappserver.pojo.dto.OrderPostData;
import com.example.sneakerappserver.service.IOrderItemService;
import com.example.sneakerappserver.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController // 接口方法返回对象 转换成json文本
@RequestMapping("/order")    // localhost:8088/user/**
public class OrderController {
    @Autowired
    IOrderService orderService;

    @Autowired
    IOrderItemService orderItemService;

    @PostMapping
    public ResponseMessage<String> createOrder(@RequestBody OrderPostData orderPostData){
        // 新增订单
        Order order = orderPostData.getOrder();

        // 检查订单是否已存在
        boolean orderExists = orderService.existsById(order.getOrderId()); // 根据主键检查是否存在
        if (orderExists) {
            return ResponseMessage.success("订单已存在，未添加新的订单项!");
        }

        // 不存在则添加订单项
        Order savedOrder = orderService.add(order);

        // 新增订单项
        List<OrderItem> orderItems = orderPostData.getOrderItems();
        orderItemService.add(orderItems);

        return ResponseMessage.success("上传订单成功!");
    }

//    @GetMapping("/{userId}")
//    public ResponseMessage<List<OrderPostData>> queryAllOrderByUserId(@PathVariable Long userId){
//        // 查询当前用户的所有订单 以及订单关联的订单项
//        // 先查询所有订单
//        List<OrderPostData> all = new ArrayList<>();
//        List<Order> orderList = orderService.queryByUserId(userId);
//
//        for(Order o: orderList){
//            //查询当前订单所对应的订单项
//            List<OrderItem> itemList = orderItemService.queryByOrderId(o.getOrderId());
//            OrderPostData op = new OrderPostData();
//            op.setOrder(o);
//            op.setOrderItems(itemList);
//            all.add(op);
//        }
//
//        return ResponseMessage.success(all);
//    }



    @GetMapping("/{userId}")        // /userId?status=
    public ResponseMessage<List<OrderPostData>> getOrdersByUserIdAndStatus(
            @PathVariable Long userId,
            @RequestParam(required = false) String status // 从查询参数中获取状态
    ) {

        // 查询当前用户的所有订单 以及订单关联的订单项
        // 先查询所有订单
        List<OrderPostData> all = new ArrayList<>();

        List<Order> orderList;

        if (status != null) {
            // 查询特定状态的订单
            orderList = orderService.queryByUserIdAndStatus(userId, status);
        } else {
            // 查询所有订单
            orderList = orderService.queryByUserId(userId);
        }

        for(Order o: orderList){
            //查询当前订单所对应的订单项
            List<OrderItem> itemList = orderItemService.queryByOrderId(o.getOrderId());
            OrderPostData op = new OrderPostData();
            op.setOrder(o);
            op.setOrderItems(itemList);
            all.add(op);
        }

        return ResponseMessage.success(all);
    }

    @GetMapping()        // /userId?status=
    public ResponseMessage<List<OrderPostData>> getOrdersByStatus(
            @RequestParam(required = true) String status // 从查询参数中获取状态
    ) {
        // 商户专用
        // 查询当前用户的所有订单 以及订单关联的订单项
        // 先查询所有订单
        List<OrderPostData> all = new ArrayList<>();

        List<Order> orderList = new ArrayList<>();

        if (status != null) {
            // 查询特定状态的订单
            orderList = orderService.queryByStatus(status);
        }

        for(Order o: orderList){
            //查询当前订单所对应的订单项
            List<OrderItem> itemList = orderItemService.queryByOrderId(o.getOrderId());
            OrderPostData op = new OrderPostData();
            op.setOrder(o);
            op.setOrderItems(itemList);
            all.add(op);
        }

        return ResponseMessage.success(all);
    }



    @DeleteMapping("/{orderId}")
    public ResponseMessage<String> deleteOrder(@PathVariable long orderId){
        Order o = orderService.queryByOrderId(orderId); // 先查询当前订单，先删除该订单关联的列表项
        List<OrderItem> itemList = orderItemService.queryByOrderId(orderId);
        orderItemService.deleteItem(itemList);  // 删除所有订单项
        // 再删除订单
        orderService.deleteOrder(o);

        return ResponseMessage.success("success");

    }

        // 查询订单状态
    @GetMapping("/query/{orderId}")
    public ResponseMessage<Order> getOrderById(@PathVariable("orderId") Long orderId) {
        Order order = orderService.queryByOrderId(orderId);
        return ResponseMessage.success(order);
    }

    // 修改订单状态
    @GetMapping("/update/{orderId}")
    public ResponseMessage<String> updateOrderStatus(@PathVariable long orderId) {
        // 修改订单状态的逻辑
        Order o = orderService.queryByOrderId(orderId); // 查询当前Id
        o.setStatus("deliver");
        o.setPaymentStatus("deliver");
        orderService.update(o);
        return ResponseMessage.success("发货成功");
    }




}
